# coding:utf-8
import requests
import json


class SendWrap:

    def __init__(self, addr):
        self.headers = {'content-type': "application/json",
                        'Authorization': 'APP appid = 4abf1a,token = 9480295ab2e2eddb8'}
        self.execute_point = [
            addr,
        ]

    @staticmethod
    def _buildBody(data):
        ojson = {}
        ojson['slaveID'] = []
        ojson['slaveID'].append(int(data[0][0]))
        ojson['slaveID'].append(int(data[0][1]))

        ojson['state'] = []
        ojson['state'].append(data[1][0])
        ojson['state'].append(data[1][1])

        ojson['tag'] = int(data[2])
        ojson['username']='hanlaoban'
        ojson['userpass']='niuX'
        all = []
        for i in data[3]:
            all.append(int(i))
        ojson['all'] = all
        body = json.dumps(ojson)
        return body

    def SendData(self, data):
        ret = None
        for i in self.execute_point:
            bodyd = self._buildBody(data)
            try:
                ret = requests.post(i, data=bodyd, headers=self.headers,timeout=(1.01,1.01), verify=False)
            except Exception as e:
                print('post exception :' + str(e.message))
                import time
                time.sleep(2)
        return ret


if __name__ == "__main__":
    a = SendWrap()
    ad = {}
    ad["id"] = -1
    r = a.SendData(ad)
    print(r.text)
